package autootje;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.border.LineBorder;

/**
 *
 * @author frankkie
 */
public class GamePanelBackUp extends JPanel {

  public BufferedImage car;
  public int dex = 1;
  public int dey = 1;
  public int direction = 0;
  public int speed = 0;

  public GamePanelBackUp() {
    this.setLayout(null);
    this.setBorder(new LineBorder(Color.black));
    this.setFocusable(true);
    this.addKeyListener(new KeyListener() {

      public void keyTyped(KeyEvent e) {
        //leegte
      }

      public void keyPressed(KeyEvent e) {
        if (e.getKeyCode() == KeyEvent.VK_LEFT) {
          draaiLinks(e.isShiftDown());
        }

        if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
          draaiRechts(e.isShiftDown());
        }

        if (e.getKeyCode() == KeyEvent.VK_UP) {
          speedUp(e.isShiftDown());
        }

        if (e.getKeyCode() == KeyEvent.VK_DOWN) {
          speedDown(e.isShiftDown());
        }
        if (e.getKeyCode() == KeyEvent.VK_SPACE) {
          speed = 0;
          doeResize();
        }
        if (e.getKeyCode() == KeyEvent.VK_PAGE_UP) {
          cL++;
          //doeResize();
        }
        if (e.getKeyCode() == KeyEvent.VK_PAGE_DOWN) {
          cL--;
          //doeResize();
        }
        if (e.getKeyCode() == KeyEvent.VK_Q) {
          twee+=0.1;
        }
        if (e.getKeyCode() == KeyEvent.VK_A) {
          twee-=0.1;
        }
        if (e.getKeyCode() == KeyEvent.VK_W) {
          drie+=0.1;
        }
        if (e.getKeyCode() == KeyEvent.VK_S) {
          drie-=0.1;
        }
      }

      public void keyReleased(KeyEvent e) {
      }
    });

    try {
      car = ImageIO.read(this.getClass().getResource("autootje.png"));
    } catch (Exception ex) {
      Main.drukaf("FOUT:\n" + ex);
    }
    this.addComponentListener(new ComponentAdapter() {

      @Override
      public void componentResized(ComponentEvent e) {
        doeResize();
        //   throw new UnsupportedOperationException("Not supported yet.");
      }
    });

    ///
    Timer klok = new Timer(100, new ActionListener() {

      public void actionPerformed(ActionEvent e) {
        doeStep();
      }
    });
    klok.setRepeats(true);
    klok.start();
  }

  public void doeStep() {
    if (speed != 0) {
      //Friction
      if (speed > 0) {
        speed--;
      } else {
        speed++;
      }

      //Limit
      if (speed > 300) {
        speed = 300;
      }
      if (speed < -300) {
        speed = -300;
      }
    }
    //Bereken de nieuwe dex en dey
    dey += (int) ((Math.sin((-direction) * (Math.PI / 180))) * speed);
    dex += (int) ((Math.cos((-direction) * (Math.PI / 180))) * speed);
    //outside-wrap
    if (dex > this.getWidth()) {
      dex -= this.getWidth();
    }
    if (dey > this.getHeight()) {
      dey -= this.getHeight();
    }
    if (dex < 0) {
      dex += this.getWidth();
    }
    if (dey < 0) {
      dey += this.getHeight();
    }
    this.repaint();
  }

  public void speedUp(boolean shift) {
    if (shift) {
      speed += 6;
    } else {
      speed += 3;
    }
  }

  public void speedDown(boolean shift) {
    if (shift) {
      speed -= 6;
    } else {
      speed -= 3;
    }
  }

  public void draaiLinks(boolean shift) {
    if (shift) {
      dir(6);
    } else {
      dir(3);
    }
  }

  public void draaiRechts(boolean shift) {
    if (shift) {
      dir(-6);
    } else {
      dir(-3);
    }
  }

  public void dir(int a) {
    direction += a;
    if (direction >= 360) {
      direction -= 360;
    }
    if (direction < 0) {
      direction += 360;
    }
    /*
    if (Main.debug) {
    Main.drukaf("Direction aangepast, is nu: " + direction);
    }*/
    this.repaint();
  }

  public void doeResize() {
    dex = (this.getWidth() / 2) - (car.getWidth() / 2);
    dey = (this.getHeight() / 2) - (car.getHeight() / 2);
    speed = 0;
    direction = 0;
    /*
    if (Main.debug){
    Main.drukaf("Ge-Re-Size-d");
    }
     */
  }
  public static int cL = 50;
  public static double twee = 2;
  public static double drie = 3;
  public static int zes = 6;

  @Override
  protected void paintComponent(Graphics g) {
    g.clearRect(0, 0, this.getWidth(), this.getHeight());
    g.drawString("Direction: " + direction + "    Speed: " + speed + "    cL: " + cL + "   twee: " + twee + "   drie: " + drie, 5, 35);
    //AUTOOTJE
    //de getallen-paren (bijv: 5-2)
    //de zijn de punten waartussen de lijnen lopen
    /*   7      3       5
     *   +------+-------+
     *   |      |       |
     * 9 +     1+-------+ 2
     *   |      |       |
     *   +------+-------+
     *   8      4       6
     */
    g.setColor(Color.black);
    //1-2
    g.drawLine(dex, dey, dex + (int) ((cL / twee) * Math.sin((direction + 90) * (Math.PI / 180))), dey + (int) ((cL / twee) * Math.cos((direction + 90) * (Math.PI / 180))));
    //3-1
    g.drawLine(dex - (int) ((cL / drie) * Math.sin((direction + 0) * (Math.PI / 180))), dey - (int) ((cL / drie) * Math.cos((direction + 0) * (Math.PI / 180))), dex, dey);
    //1-4
    g.drawLine(dex, dey, dex + (int) ((cL / drie) * Math.sin((direction + 0) * (Math.PI / 180))), dey + (int) ((cL / drie) * Math.cos((direction + 0) * (Math.PI / 180))));
    //5-2
    g.drawLine((dex + (int) ((cL / twee) * Math.sin((direction + 90) * (Math.PI / 180)))) - ((int) ((cL / drie) * Math.sin((direction + 0) * (Math.PI / 180)))), (dey - (int) ((cL / drie) * Math.cos((direction + 0) * (Math.PI / 180)))) + ((int) ((cL / twee) * Math.cos((direction + 90) * (Math.PI / 180)))), dex + (int) ((cL / twee) * Math.sin((direction + 90) * (Math.PI / 180))), dey + (int) ((cL / twee) * Math.cos((direction + 90) * (Math.PI / 180))));
    //6-2
    g.drawLine((dex + (int) ((cL / twee) * Math.sin((direction + 90) * (Math.PI / 180)))) + ((int) ((cL / drie) * Math.sin((direction + 0) * (Math.PI / 180)))), (dey + (int) ((cL / drie) * Math.cos((direction + 0) * (Math.PI / 180)))) + ((int) ((cL / twee) * Math.cos((direction + 90) * (Math.PI / 180)))), dex + (int) ((cL / twee) * Math.sin((direction + 90) * (Math.PI / 180))), dey + (int) ((cL / twee) * Math.cos((direction + 90) * (Math.PI / 180))));
    //9-9
    g.drawLine(dex - (int) ((cL / twee) * Math.sin((direction + 90) * (Math.PI / 180))), dey - (int) ((cL / twee) * Math.cos((direction + 90) * (Math.PI / 180))), dex - (int) ((cL / twee) * Math.sin((direction + 90) * (Math.PI / 180))), dey - (int) ((cL / twee) * Math.cos((direction + 90) * (Math.PI / 180))));
    //7-9
    g.drawLine((dex - (int) ((cL / twee) * Math.sin((direction + 90) * (Math.PI / 180)))) - ((int) ((cL / drie) * Math.sin((direction - 0) * (Math.PI / 180)))), (dey - (int) ((cL / drie) * Math.cos((direction - 0) * (Math.PI / 180)))) + ((int) ((cL / twee) * Math.cos((direction - 90) * (Math.PI / 180)))), dex - (int) ((cL / twee) * Math.sin((direction + 90) * (Math.PI / 180))), dey - (int) ((cL / twee) * Math.cos((direction + 90) * (Math.PI / 180))));
    //8-9
    g.drawLine((dex - (int) ((cL / twee) * Math.sin((direction + 90) * (Math.PI / 180)))) + ((int) ((cL / drie) * Math.sin((direction - 0) * (Math.PI / 180)))), (dey + (int) ((cL / drie) * Math.cos((direction - 0) * (Math.PI / 180)))) + ((int) ((cL / twee) * Math.cos((direction - 90) * (Math.PI / 180)))), dex - (int) ((cL / twee) * Math.sin((direction + 90) * (Math.PI / 180))), dey - (int) ((cL / twee) * Math.cos((direction + 90) * (Math.PI / 180))));
    //7-3
    g.drawLine((dex - (int) ((cL / twee) * Math.sin((direction + 90) * (Math.PI / 180)))) - ((int) ((cL / drie) * Math.sin((direction - 0) * (Math.PI / 180)))), (dey - (int) ((cL / drie) * Math.cos((direction - 0) * (Math.PI / 180)))) + ((int) ((cL / twee) * Math.cos((direction - 90) * (Math.PI / 180)))), dex - (int) ((cL / drie) * Math.sin((direction + 0) * (Math.PI / 180))), dey - (int) ((cL / drie) * Math.cos((direction + 0) * (Math.PI / 180))));
    //8-4
    g.drawLine(dex + (int) ((cL / drie) * Math.sin((direction + 0) * (Math.PI / 180))), dey + (int) ((cL / drie) * Math.cos((direction + 0) * (Math.PI / 180))), (dex - (int) ((cL / twee) * Math.sin((direction + 90) * (Math.PI / 180)))) + ((int) ((cL / drie) * Math.sin((direction - 0) * (Math.PI / 180)))), (dey + (int) ((cL / drie) * Math.cos((direction - 0) * (Math.PI / 180)))) + ((int) ((cL / twee) * Math.cos((direction - 90) * (Math.PI / 180)))));
    //5-3
    g.drawLine((dex + (int) ((cL / twee) * Math.sin((direction + 90) * (Math.PI / 180)))) - ((int) ((cL / drie) * Math.sin((direction + 0) * (Math.PI / 180)))), (dey - (int) ((cL / drie) * Math.cos((direction + 0) * (Math.PI / 180)))) + ((int) ((cL / twee) * Math.cos((direction + 90) * (Math.PI / 180)))), dex - (int) ((cL / drie) * Math.sin((direction + 0) * (Math.PI / 180))), dey - (int) ((cL / drie) * Math.cos((direction + 0) * (Math.PI / 180))));
    //6-4
    g.drawLine((dex + (int) ((cL / twee) * Math.sin((direction + 90) * (Math.PI / 180)))) + ((int) ((cL / drie) * Math.sin((direction + 0) * (Math.PI / 180)))), (dey + (int) ((cL / drie) * Math.cos((direction + 0) * (Math.PI / 180)))) + ((int) ((cL / twee) * Math.cos((direction + 90) * (Math.PI / 180)))), (dex - (int) ((cL / twee) * Math.sin((direction + 90) * (Math.PI / 180)))) + ((int) ((cL / drie) * Math.sin((direction - 0) * (Math.PI / 180)))), (dey + (int) ((cL / drie) * Math.cos((direction - 0) * (Math.PI / 180)))) + ((int) ((cL / twee) * Math.cos((direction - 90) * (Math.PI / 180)))));
    //g.drawLine(dex + (int) ((cL / twee) * Math.sin((direction + 90) * (Math.PI / 180)))) + ((int) ((cL / drie) * Math.sin((direction + 0) * (Math.PI / 180)))), (dey + (int) ((cL / drie) * Math.cos((direction + 0) * (Math.PI / 180)))) + ((int) ((cL / twee) * Math.cos((direction + 90) * (Math.PI / 180)))),0,0);
    //g.drawLine(dex,dey,dex,dey); //Puntje in het Midden
    //1-1
    g.setColor(Color.red);
    g.fillOval(dex - 4, dey - 4, 8, 8); //Rondje in het Midden
  }
}
